package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.MallItemEntity;
import com.zw.yzk.kabu.data.network.task.MallItemTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.mapper.MallItemMapper;
import com.zw.yzk.kabu.view.view.MallView;


import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/2/26.
 */

public class MallPresenter implements Presenter {

    private UseCase getGoods;
    private MallView mallView;
    private AppCompatActivity activity;
    private MallItemMapper goodsMapper;

    @Inject
    public MallPresenter(@Named("goodsUseCase") UseCase getGoods,
                         AppCompatActivity activity,
                         MallItemMapper goodsMapper) {
        this.getGoods = getGoods;
        this.activity = activity;
        this.goodsMapper = goodsMapper;
    }

    public void setView(MallView view) {
        this.mallView = view;
    }

    public void getGoods(int start, boolean getLocal) {
        getGoods.execute(new GoodsListSubscriber(activity), new MallItemTask(getLocal));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getGoods.unSubscribe();
    }

    private class GoodsListSubscriber extends BaseSubscriber<MallItemEntity> {

        public GoodsListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(MallItemEntity entity) {
            super.onNext(entity);
            mallView.getGoodsSuccess(goodsMapper.transfer(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mallView.getGoodsFailed();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_goods_list_failed);
        }
    }
}
